<?php
/**
 * 图书管理控制器
 * 
 * @author sandy
 *
 */
class bookApp extends adminControl{
	
	protected function _init(){
		$this->lang->load('book');
	}
	
	/**
	 * 图书列表
	 */
	public function index(){
		$book_model = &get_model('book');
		
		// 获取当前页码及分页配置
		$page_info = get_page(20);
		$books = $book_model->get_all_book($page_info);
		
		// 获取分页信息
		$page_info['total'] = $books['total'];
		$url = "index.php?c=book";
		$pages_data = format_page($page_info['total'], $page_info['cur_page'], $url, $page_info['per_page'],5);
		$pages = get_page_html($pages_data);
		
		$this->assign('books',$books['result']);
		$this->assign('pages',$pages);
		
		$this->_title = $this->lang->get('html_title') . ' - ' . $this->lang->get('app_name');
		$this->display();
	}
	
	/**
	 * 编辑图书
	 */
	public function edit(){

		$dosubmit = isset($_REQUEST['dosubmit']) ? $_REQUEST['dosubmit'] : false;
		
		if ($dosubmit){
			
			$book_name = isset($_POST['name']) ? trim($_POST['name']) : "";
			$author_id = isset($_POST['author_id']) ? intval($_POST['author_id']) : "";
			$category_id =  isset($_POST['category_id']) ? intval($_POST['category_id']) : "";
			$book_structure_type = isset($_POST['structure_type']) ? intval($_POST['structure_type']) : "";
			$abstract = isset($_POST['name']) ? trim($_POST['abstract']) : "";
			$book_id = isset($_POST['book_id']) ? intval($_POST['book_id']) : 0;
			
			$cover = $_FILES['cover'];
			
			if ($cover['size']) {
				// 处理上传图片及生成缩略图
				import_lib('upload.upload');
				$upload = new upload($cover);
				$upload->allowed = "image/*";
				//$upload->language = '';
	
				if ($upload->uploaded) {
					$upload->file_new_name_body = md5($cover['name'].time());
					$upload->file_name_body_add = 'cover';
					$upload->Process(BASE_PATH);
					$image_source = $upload->file_dst_name;
					
					// 生成缩略图
					$upload->file_new_name_body = md5($cover['name'].time());
					$upload->file_name_body_add = 'thumb';
					$upload->image_resize            = true;
					$upload->image_ratio_fill		= true;
					$upload->image_x                 = 100;
					$upload->image_y                 = 148;
					$upload->Process(BASE_PATH.'/thumb/');
					
					$image_thumb =  $upload->file_dst_name;
					$upload-> Clean();
				} else {
					echo $upload->error;
					exit();
				}
			}
			
			$book_info = array(
					'book_id' => $book_id,
					'book_name' => $book_name,
					'author_id' => $author_id,
					'category_id' => $category_id,
					'structure_id' => $book_structure_type,
					'abstract' => $abstract,
			);
			
			$book_mod = &get_model('book');
			
			$return_url = $this->config->get_item('site_url').'/index.php?c=book';
			if($book_mod->update_book($book_info)){
				$this->show_message( "编辑图书成功!",$return_url);
			} else {
				$this->show_message("编辑图书失败!",$return_url);
			}
			
		} else {
			$book_id = isset($_GET['bid']) ? intval($_GET['bid']) : 0;
			// 图书信息
			$book_mod = &get_model('book');
			$book_info = $book_mod->get_book_info($book_id);
			//dump($book_info);
			
			// 作者信息
			$author_mod = &get_model('author');
			$author= $author_mod->get_all_author();
			
			// 分类信息
			$category_mod = &get_model('category');
			$categories = $category_mod->get_category();
			// 分类信息树形结构
			$category_tree = $category_mod->get_category_tree($categories['result'],$book_info['category_id']);
			
			// 内容结构
			$common_mod = &get_model('common');
			$book_structure = $common_mod->get_book_structure();
				
			$this->assign('book_structure',$book_structure);
			$this->assign('author',$author['result']);
			$this->assign('category_tree',$category_tree);
			$this->assign('book_info',$book_info);
			$this->display();
		}
		
	}
	
	/**
	 * 添加图书
	 */
	public function add(){
		$dosubmit = isset($_REQUEST['dosubmit']) ? $_REQUEST['dosubmit'] : false;
		
		if ($dosubmit){
			$book_name = isset($_POST['name']) ? trim($_POST['name']) : "";
			$author_id = isset($_POST['author_id']) ? intval($_POST['author_id']) : "";
			$category_id =  isset($_POST['category_id']) ? intval($_POST['category_id']) : "";
			$book_structure_type = isset($_POST['structure_type']) ? intval($_POST['structure_type']) : "";
			$abstract = isset($_POST['name']) ? trim($_POST['abstract']) : "";
			
			$book_info = array(
					'book_name' => $book_name,
					'author_id' => $author_id,
					'category_id' => $category_id,
					'structure_id' => $book_structure_type,
					'abstract' => $abstract,
			);	
			
			$book_mod = &get_model('book');
				
			$return_url = $this->config->get_item('site_url').'/index.php?c=book';
			if($book_mod->add_book($book_info)){
				$this->show_message( "添加图书成功!",$return_url);
			} else {
				$this->show_message("添加图书失败!",$return_url);
			}
				
		} else {
			// 作者信息
			$author_mod = &get_model('author');
			$author= $author_mod->get_all_author();
			
			// 分类信息
			$category_mod = &get_model('category');
			$categories = $category_mod->get_category();
			// 分类信息树形结构
			$category_tree = $category_mod->get_category_tree($categories['result']);
			
			// 内容结构
			$common_mod = &get_model('common');
			$book_structure = $common_mod->get_book_structure();
			
			$this->assign('book_structure',$book_structure);
			$this->assign('author',$author['result']);
			$this->assign('category_tree',$category_tree);
			$this->display();
		}
	}
	
	/**
	 * 删除图书
	 */
	public function del(){
		
	}
}